package com.adxon;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang3.StringEscapeUtils;

import com.adxon.servlet.ServletBase;

@SuppressWarnings("serial")
public class MsgException extends Exception{
	public final static int ACCOUNT			=1;
	public final static int EXIST			=2;
	public final static int ILLEGAL_ACCESS	=3;
	public final static int INSTANTIATION	=4;
	public final static int NOT_FOUND		=5;
	public final static int NOT_MATCH		=6;
	public final static int PASSWORD		=7;
	public final static int REUPDATE		=8;
	public final static int SQL				=9;

	public static void message(HttpServletRequest req,JspWriter out)throws IOException{
		String m=(String)req.getAttribute("Message");
		if(m==null)
			return;
		out.write("<div class=msg>");
		out.write(m);
		out.write("</div>");
	}

	private int cod;
	private String fld;
	private String[] msg={
			null,				// 0
			"Account.",			// 1
			"Exist.",			// 2
			"Illegal access.",	// 3
			"Instantiation.",	// 4
			"Not found.",		// 5
			"Not match",		// 6
			"Password",			// 7
			"Reupdate",			// 8
			"Sql",				// 9
		};

	public MsgException(int code,String field){
		super();
		cod=code;
		fld=field;
	}
	public int getCode(){
		return cod;
	}
	public String getMessage(){
		return msg[cod];
	}
	public void getMessage(HttpServletRequest req){
		req.setAttribute("Message",StringEscapeUtils.escapeHtml4(
				getMessage()));
		req.setAttribute("Field",StringEscapeUtils.escapeHtml4(fld));
	}
	public void routing(HttpServletRequest req,HttpServletResponse resp)
			throws IOException, ServletException{
		switch(cod){
		case MsgException.ACCOUNT:
		case MsgException.EXIST:
		case MsgException.NOT_FOUND:
		case MsgException.NOT_MATCH:
		case MsgException.PASSWORD:
		default:
			ServletBase.forward404(req,resp);
		}
	}
}
